/*
 * TimeSelector.java
 * 
 * Created on Nov 14, 2007, 9:23:31 PM
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.rtemaster.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Calendar;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import org.fpdev.util.FPUtil;
import org.fpdev.util.gui.GUIFactory;

/**
 *
 * @author demory
 */
public class TimeSelector extends JPanel implements FocusListener {

  private JTextField hourField_,  minField_;
  private JComboBox ampm_;
  private TimeSelectorChangeListener change_;
  public final int FIELD_HOUR = 1;
  public final int FIELD_MINUTE = 2;

  public TimeSelector() {
    this(null);
  }

  public TimeSelector(TimeSelectorChangeListener change) {
    super();
    change_ = change;

    setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

    hourField_ = GUIFactory.newTextField("", 2, 20); //new JTextField(""+cal.get(Calendar.HOUR), 2);
    hourField_.setHorizontalAlignment(JTextField.CENTER);

    final JPopupMenu hourPMenu = new JPopupMenu();
    for (int h = 1; h <= 12; h++) {
      new AdjustTimeMenuItem(FIELD_HOUR, h, hourPMenu);
    }
    hourField_.addMouseListener(new MouseAdapter() {

      @Override
      public void mousePressed(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON3) {
          hourPMenu.show(hourField_, hourField_.getWidth(), 0);
        }
      }
    });
    hourField_.addFocusListener(this);

    minField_ = GUIFactory.newTextField("", 2, 20); //new JTextField(""+cal.get(Calendar.MINUTE), 2);
    minField_.setHorizontalAlignment(JTextField.CENTER);

    final JPopupMenu minPMenu = new JPopupMenu();
    for (int m = 0; m <= 11; m++) {
      new AdjustTimeMenuItem(FIELD_MINUTE, m * 5, minPMenu);
    }
    minField_.addMouseListener(new MouseAdapter() {

      @Override
      public void mousePressed(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON3) {
          minPMenu.show(minField_, minField_.getWidth(), 0);
        }
      }
    });
    minField_.addFocusListener(this);

    ampm_ = GUIFactory.newComboBox(45);
    ampm_.addItem("AM");
    ampm_.addItem("PM");
    ampm_.addFocusListener(this);

    this.add(hourField_);
    this.add(new JLabel(":"));
    this.add(minField_);
    this.add(Box.createHorizontalStrut(3));
    this.add(ampm_);

    Calendar cal = Calendar.getInstance();
    int now = (Math.max(0, cal.get(Calendar.HOUR_OF_DAY) - 1)) * 60 + cal.get(Calendar.MINUTE);
    updateTime(now);
  }

  @Override
  public void focusGained(FocusEvent e) {
  }

  @Override
  public void focusLost(FocusEvent e) {
    if (change_ != null) {
      change_.timeSelectorChanged();
    }
  //updateCurrentTime();
  }

  private void adjustTime(int field, int value) {
    if (field == FIELD_HOUR) {
      hourField_.setText("" + value);
    }
    if (field == FIELD_MINUTE) {
      minField_.setText("" + value);
    }
    if (change_ != null) {
      change_.timeSelectorChanged();
    }
  //updateCurrentTime();
  }

  public void updateTime(int timeMin) {
    //System.out.println("time "+timeMin);
    timeMin = (timeMin+1440) % 1440;
    int hours = (timeMin / 60) % 12;
    hourField_.setText(hours == 0 ? "12" : "" + hours);
    int mins = timeMin % 60;
    minField_.setText(mins < 10 ? "0" + mins : "" + mins);
    ampm_.setSelectedIndex(timeMin / 720);
  }

  public int getSTime() {
    return FPUtil.getSTime(new Integer(hourField_.getText()).intValue(), new Integer(minField_.getText()).intValue(), ampm_.getSelectedIndex() + 1);
  }

  public int getMTime() {
    int hour = new Integer(hourField_.getText()).intValue();
    int min = new Integer(minField_.getText()).intValue();

    if (hour == 12 && ampm_.getSelectedIndex() == 0) {
      hour = 0;
    } // 12 midnight
    else if (hour != 12 && ampm_.getSelectedIndex() == 1) {
      hour += 12;
    } // 1pm to 11pm
    return hour * 60 + min;
  }

  private class AdjustTimeMenuItem extends JMenuItem {

    //private int field_, value_;
    public AdjustTimeMenuItem(final int field, final int value, JPopupMenu menu) {
      super("" + value);
      setFont(GUIFactory.MAIN_FONT);
      menu.add(this);
      this.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
          adjustTime(field, value);
        }
      });
    }
  }

  public interface TimeSelectorChangeListener {

    public void timeSelectorChanged();
  }
}
